faApp.controller('MediaRecommendListCtrl', ['$scope', '$location', '$controller', '$route', 'productService', 'mediaService', 'simuService', function($scope, $location, $controller, $route, productService, mediaService, simuService) {
  $controller('BaseListCtrl', { $scope: $scope });

  $scope.list = [];
  $scope.loading = false;

  $scope.$watch('config', function() {
    mediaService.mediaRecommendList($scope.config).then(function(json){
      $scope.list = json.list;
      $scope.pager = json.page;
    });
  }, true);
 
  $scope.config = {
    "pageSize": 20,
    "pageNo": 1
  };

  $scope.meida = {productType: '', productCode: '', productName: '', mediaSource: '', productList: {}};

  var tmpType = '';
  $scope.$watch('meida.productType', function() {
    if($scope.meida.productType && ($scope.meida.productType == '04')){
      alert('暂不支持此类型！');
      $scope.meida.productType = tmpType;
      return;
    }

    if($scope.meida.productType && (tmpType != $scope.meida.productType)){
      $scope.loading = true;
      tmpType = $scope.meida.productType;
      var param = {
        pageNo:1, 
        pageSize:1000,
        pTypeList:$scope.meida.productType, 
        pPublicStatus:'2'
      };
      // 根据类型查询对应的产品列表
      if($scope.meida.productType == '06'){
        // PE/VC
        productService.queryProductList(param).then(function(json){
          $scope.meida.productList = json.list;
          // 删除已经添加的！
          exclude();

          $scope.loading = false;
        });
      }else if($scope.meida.productType == '02' || $scope.meida.productType == '03'|| $scope.meida.productType == '07'){
        // 信托资管
        productService.productList(param).then(function(json){
          $scope.meida.productList = json.projectList;
          
          // 删除已经添加的！
          exclude();
          
          $scope.loading = false;
        });
      }else if($scope.meida.productType == '01'){
        // 阳光私募
        simuService.list(param).then(function(json){
          $scope.meida.productList = json.list;
          
          // 删除已经添加的！
          exclude();
          $scope.loading = false;
        });
      }
    }
  }, true);

  $scope.mediaAddRecommend = function(){
    if($scope.meida.productCode && $scope.meida.productType && $scope.meida.recommentType){
      var o = {
        productType: $scope.meida.productType,
        productCode: $scope.meida.productCode,
        recommentType : $scope.meida.recommentType

      };

      mediaService.mediaRecommendAdd(o).then(function(json){
        if(!json.statusCode){
          mediaService.mediaRecommendList($scope.config).then(function(json){
            $scope.list = json.list;
            $scope.pager = json.page;
            // 删除已经添加的！
            exclude();

            $scope.loading = false;
          });
        }else{
          if(json.statusCode == '2003'){
            alert('重复添加！');
          }else if(json.statusCode == '2009'){
            alert('产品不存在！');
          }else{
            alert('error' + json.statusCode);
          }
        }
      });
    }else{
      alert('请选择产品和类型！');
    }
  }

  $scope.delete = function(id) {
    if(confirm('确定删除吗！')){
      mediaService.mediaRecommendDelete({ id: id }).then(function(json) {
        if(!json.statusCode){
          alert('删除成功！');
          $route.reload();
        }else{
          alert('error：' + json.statusCode);
          $route.reload();
        }
      });
    }
  };

  function exclude() {
    var tmp = angular.copy($scope.meida.productList);
    var c = 0;
    var id = 0;
    angular.forEach(tmp, function(i, o) {
      id = i.pCode || i.id;
      angular.forEach($scope.list, function(j) {
        if (id == j.productCode) {
          $scope.meida.productList.splice((o - c), 1);
          c++;
          $scope.meida.productCode = '';
        }
      })
    });
  }

}]);
